package com.gaosai.staff.commlistadapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.answer_staff.R;
import com.example.answer_staff.RegisterActivity;
import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.view.CustomFlowLayout;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ShowActivityAdapter extends BaseAdapter {
	private Context context;
	private DisplayImageOptions options; 
	private ArrayList<String>showActivity_list;
	private String subject_activity_show;
	private String briefContent_activity_show;
	private int start_time_activity_show;
	private int end_time_activity_show;
	private String venue_activity_show;
	private JSONArray activityTags;
	private LayoutInflater mInflater ;
	private String pic_banner;
	public ShowActivityAdapter(Context context,ArrayList<String>list) {
		this.context=context;
		this.showActivity_list=list;
		mInflater = LayoutInflater.from(context) ;
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_launcher)
		.showImageForEmptyUri(R.drawable.ic_launcher)
		.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
		.cacheOnDisc(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
	
	
	}

	@Override
	public int getCount() {
		return showActivity_list.size();

	}

	@Override
	public Object getItem(int position) {
		return showActivity_list.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}
	@Override
	public View getView( final int position, View convertView, ViewGroup parent) {
		System.out.println("z的长度是"+showActivity_list.size());
		String s=showActivity_list.get(position);
		System.out.println("getViewde s是"+s);
		JsonParse(s);
		ViewHolder  viewholder=null;
		if(convertView==null){
			convertView=LayoutInflater.from(context).inflate(R.layout.activity_item, null);
			viewholder = new ViewHolder();
			viewholder.pic_activity=(ImageView)convertView.findViewById(R.id.imageView_activity);
			viewholder.subject_activity = (TextView) convertView.findViewById(R.id.title) ;
			viewholder.briefContent_activity = (TextView) convertView.findViewById(R.id.content_bf) ;
			viewholder.start_time_activity = (TextView) convertView.findViewById(R.id.time) ;
			viewholder.end_time_activity = (TextView) convertView.findViewById(R.id.date) ;
			viewholder.venue_activity = (TextView) convertView.findViewById(R.id.venue) ;
			viewholder.regist_activity=(RelativeLayout)convertView.findViewById(R.id.regis_bottom_listeners_activity);
			viewholder.activityFlowLayout = (CustomFlowLayout) convertView.findViewById(R.id.activity_flowlayout) ;


			convertView.setTag(viewholder);
		}else{
			viewholder=(ViewHolder)convertView.getTag();
		}
		ImageLoader.getInstance().displayImage(pic_banner, viewholder.pic_activity,options) ;
		viewholder.subject_activity.setText(subject_activity_show);
		viewholder.briefContent_activity.setText(briefContent_activity_show);
		viewholder.start_time_activity.setText(CommonUtils.getDateToStringt(start_time_activity_show));
		viewholder.end_time_activity.setText(CommonUtils.getDateToStringt(end_time_activity_show));
		viewholder.venue_activity.setText(venue_activity_show+"");
		viewholder.regist_activity.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(context,RegisterActivity.class);
				intent.putExtra("application_list", showActivity_list.get(position));
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		});
		
		viewholder.activityFlowLayout.removeAllViews() ;//先把布局流中的标签清空，再添加，否则出出现 滚动的时候重复添加
		for (int i = 0; i < activityTags.length(); i++) {
			TextView tv = (TextView) mInflater.inflate(R.layout.tv,viewholder.activityFlowLayout, false);
			String tagName = activityTags.optJSONObject(i).optString("name") ;
			//取得缩写名字
			String abbreName = getAbbreviation(tagName) ;
			tv.setText(abbreName) ;
			setColor(abbreName,tv) ;
			viewholder.activityFlowLayout.addView(tv) ;
	
		}
			return convertView;
	}

	private void setColor(String text, TextView tv) {
		if("HR".equals(text)){
			tv.setBackgroundColor(context.getResources().getColor(R.color.hr_color)) ;
		}else if("TD".equals(text)){
			tv.setBackgroundColor(context.getResources().getColor(R.color.td_color)) ;
		}else if("GTU".equals(text)){
			tv.setBackgroundColor(context.getResources().getColor(R.color.gtu_color)) ;
		}else if("INNO".equals(text)){
			tv.setBackgroundColor(context.getResources().getColor(R.color.inno_color)) ;
		}else if("OPS".equals(text)){
			tv.setBackgroundColor(context.getResources().getColor(R.color.ops_color)) ;
		}else if("CS".equals(text)){
			tv.setBackgroundColor(context.getResources().getColor(R.color.cs_color)) ;
		}else if("FIN".equals(text)){
			tv.setBackgroundColor(context.getResources().getColor(R.color.fin_color)) ;
		}else if("COMM".equals(text)){
			tv.setBackgroundColor(context.getResources().getColor(R.color.comm_color));
		}
	
	
	
	
	
	}

	private String getAbbreviation(String inputString) {

		String outPutString = null ;
		if("Technical Development".equals(inputString)){
			outPutString = "TD" ;
		}else if("Human Resources".equals(inputString)){
			outPutString = "HR" ;
		}else if("Trade Union".equals(inputString)){
			outPutString = "GTU" ;
		}else if("Innovation".equals(inputString)){
			outPutString = "INNO" ;
		}else if("Center Operations".equals(inputString)){
			outPutString = "OPS" ;
		}else if("Corporate Sustainability".equals(inputString)){
			outPutString = "CS" ;
		}else if("Finance".equals(inputString)){
			outPutString = "FIN" ;
		}else if("Communications".equals(inputString)){
			outPutString = "COMM" ;
		}
		return outPutString ;
		
	}

	private void JsonParse(String json) {
		JSONObject jsonObject=null;
		try {
			jsonObject = new JSONObject(json);
			subject_activity_show=jsonObject.getString("subject");
			briefContent_activity_show=jsonObject.getString("briefContent");
			start_time_activity_show=jsonObject.getInt("startTime");
			end_time_activity_show=jsonObject.getInt("endTime");
			venue_activity_show=jsonObject.getString("venue");
			activityTags = jsonObject.optJSONArray("tags") ;
			pic_banner=jsonObject.getString("banner");
			System.out.println("show解析得知是"+subject_activity_show+briefContent_activity_show+end_time_activity_show);

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	class ViewHolder{
		private CustomFlowLayout activityFlowLayout ;
		private TextView subject_activity ;
		private TextView briefContent_activity ;
		private TextView start_time_activity ;
		private TextView end_time_activity ;
		private TextView venue_activity ;	
		private RelativeLayout regist_activity ;
		private ImageView pic_activity;


	}









}
